package com.xingtu.testSerialport.utils;

import com.xingtu.testSerialport.MyApplication;
import com.xingtu.testSerialport.R;

import java.util.Calendar;

//一些公共常用函数;
public class CommonFunction {


    //解析整型数;
    public static int parseInt(String strValue)
    {
        return parseInt(strValue, 0);
    }

    public static int parseInt(String strValue, int defaultValue)
    {
        try
        {
            return Integer.parseInt(strValue);
        }
        catch (Exception e)
        {
            return defaultValue;
        }
    }

    // 解析长整型数;
    public static long parseLong(String strValue)
    {
        try
        {
            return Long.parseLong(strValue);
        }
        catch (Exception e)
        {
            return 0L;
        }
    }

    //解析浮点数;
    public static double parseDouble(String strValue)
    {
        try
        {
            return Double.parseDouble(strValue);
        }
        catch (Exception e)
        {
            return 0;
        }
    }

    public static double parseGGAAngle(String strValue){
        double result=0;
        if (strValue==null){
            return result;
        }
        if (strValue.length()==0){
            return result;
        }

        boolean isNegative=strValue.startsWith("-");
        if (isNegative){
            strValue=strValue.substring(1);
        }

        int index=strValue.indexOf('.');
        if (index==-1){
            result = parseDouble(strValue);
        }
        else{
            if (index>2){
                int degree=parseInt(strValue.substring(0, index-2));
                double minute=parseDouble(strValue.substring(index-2))/60.0;
                result=degree+minute;
            }
            else{
                result=parseDouble(strValue)/60.0;
            }
        }

        if (isNegative){
            result=-result;
        }

        return result;
    }

    public static int getZoneTimeOffset(Calendar calendar)
    {
        int zoneOffset = Calendar.getInstance().get(Calendar.ZONE_OFFSET);
        int dstOffset = calendar.get(Calendar.DST_OFFSET);
        return zoneOffset + dstOffset;
    }


    public static String getStats(int a) {
        String result = "";
        switch (a) {
            case 0:
                result = MyApplication.getInstance().getResources().getString(R.string.commonfunction_csh);
                break;
            case 1:
                result = MyApplication.getInstance().getResources().getString(R.string.commonfunction_dddw);
                break;
            case 2:
                result = MyApplication.getInstance().getResources().getString(R.string.commonfunction_mcf);
                break;
            case 3:
                result = MyApplication.getInstance().getResources().getString(R.string.commonfunction_wxpps);
                break;
            case 4:
                result = MyApplication.getInstance().getResources().getString(R.string.commonfunction_gdj);
                break;
            case 5:
                result = MyApplication.getInstance().getResources().getString(R.string.commonfunction_fdj);
                break;
            case 6:
                result =  MyApplication.getInstance().getResources().getString(R.string.commonfunction_zzgs);
                break;
            case 7:
                result = MyApplication.getInstance().getResources().getString(R.string.commonfunction_rgsrgdz);
                break;
            case 8:
                result = MyApplication.getInstance().getResources().getString(R.string.commonfunction_mnms);
                break;
            case 9:
                result = MyApplication.getInstance().getResources().getString(R.string.commonfunction_waascf);
                break;
        }
        return result;
    }

}
